package com.miracle.flexAPI.api.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.miracle.flexAPI.api.dto.CacheUserDto;
import com.miracle.flexAPI.api.entity.CacheUser;
import com.miracle.flexAPI.api.service.CacheUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/CacheUser")
@Api(value = "takApi",tags = {"用户缓存（测试）"})
public class CacheUserController {

    @Autowired
    RedisTemplate redisTemplate;
    @Resource
    CacheUserService service;
    @Autowired
    RedisCache redisCache;


    @GetMapping(value = "list")
    @ApiOperation(value = "查询（缓存保存List）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "name" , value="用户名", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "tel" , value="联系方式", defaultValue = "",dataType = "Integer",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult list(@ApiIgnore CacheUserDto dto){
        return AjaxResult.success(service.selectUser(dto));
    }


    @GetMapping(value = "listJsonString")
    @ApiOperation(value = "查询缓存（缓存保存字符串）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "name" , value="用户名", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "tel" , value="联系方式", defaultValue = "",dataType = "Integer",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult listJson(@ApiIgnore CacheUserDto dto){
       return service.selectUserJson(dto);
    }


    @GetMapping(value = "listCT")
    @ApiOperation(value = "查询缓存（缓存穿透解决方案）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "name" , value="用户名", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "tel" , value="联系方式", defaultValue = "",dataType = "Integer",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult listCT(@ApiIgnore CacheUserDto dto){
        return service.selectUserCT(dto);
    }



    @GetMapping(value = "listJC")
    @ApiOperation(value = "查询缓存（缓存击穿解决方案）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "name" , value="用户名", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "tel" , value="联系方式", defaultValue = "",dataType = "Integer",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult listJC(@ApiIgnore CacheUserDto dto){
        return service.selectUserJC(dto);
    }




    @GetMapping(value = "deleteUserCache")
    @ApiOperation(value = "根据ID删除缓存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult deleteUserCache(@ApiIgnore CacheUserDto dto){
        return AjaxResult.success(service.deleteCacheById(dto));
    }

    @GetMapping(value = "getById")
    @ApiOperation(value = "根据ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult getUser(@ApiIgnore CacheUserDto dto){
        return AjaxResult.success(service.findById(dto));
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId" , value="部门ID", defaultValue = "1",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "userName" , value="用户名", defaultValue = "",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "nickName" , value="昵称", defaultValue = "",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "userType" , value="类型", defaultValue = "1",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "email" , value="邮箱", defaultValue = "123@qwe",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "phonenumber" , value="联系方式", defaultValue = "12345678910",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "sex" , value="性别", defaultValue = "男",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "password" , value="密码", defaultValue = "123456",dataType = "string",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult insertUser(@ApiIgnore CacheUser model){
        return AjaxResult.success(service.insertUser(model));
    }

    @PostMapping(value = "addByMap")
    @ApiOperation(value = "添加（使用map传参）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId" , value="部门ID", defaultValue = "1",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "userName" , value="用户名", defaultValue = "",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "nickName" , value="昵称", defaultValue = "",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "userType" , value="类型", defaultValue = "1",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "email" , value="邮箱", defaultValue = "123@qwe",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "phonenumber" , value="联系方式", defaultValue = "12345678910",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "sex" , value="性别", defaultValue = "男",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "password" , value="密码", defaultValue = "123456",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "ccc" , value="ccc", defaultValue = "123456",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "ccc[aaa]" , value="aaa ", defaultValue = "123456",dataType = "string",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult addByMap(@ApiIgnore CacheUser model, @ApiIgnore @RequestParam Map<String, String> model2){
        return AjaxResult.success(model);
    }

    @PostMapping(value = "update")
    @ApiOperation(value = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "deptId" , value="部门ID", defaultValue = "1",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "userName" , value="用户名", defaultValue = "",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "nickName" , value="昵称", defaultValue = "",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "userType" , value="类型", defaultValue = "1",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "email" , value="邮箱", defaultValue = "123@qwe",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "phonenumber" , value="联系方式", defaultValue = "12345678910",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "sex" , value="性别", defaultValue = "男",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "password" , value="密码", defaultValue = "123456",dataType = "string",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult edit(@ApiIgnore CacheUser model){
        return AjaxResult.success(service.updateUser(model));
    }


    @PostMapping(value = "delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="主键-唯一标识", defaultValue = "",dataType = "Integer",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult delete(@ApiIgnore CacheUserDto dto){
        return AjaxResult.success(service.deleteUser(dto));
    }


    @PostMapping(value = "testRedis")
    @ApiOperation(value = "测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value="id", defaultValue = "",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "name" , value="名称", defaultValue = "",dataType = "String",paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code=200,message =
                    "data:保存的数据\n"
            )
    })
    public AjaxResult testRedis(@ApiIgnore CacheUserDto dto){
        redisCache.setCacheObject(dto.getId()+"o",dto);
        redisCache.setCacheMap(dto.getId()+"m", JSON.parseObject(JSON.toJSONString(dto), Map.class));
        redisCache.setCacheObject(dto.getId()+"s",dto.getName());

        return AjaxResult.success("-");
    }

}
